#include <learn/check_gl.h>
#include <learn/gl/gl_callback.h>

#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>

using namespace xi::gl;

void init()
{
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    // 指定 4 种颜色纹理
    float texLine[] = {0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1};

    // 指定边界混合模式
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    // 指定一维纹理图像
    glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 4, 0, GL_RGBA, GL_FLOAT, texLine);

    // 激活纹理
    glEnable(GL_TEXTURE_1D);

    // 指定 16 x 16 颜色纹理
    float texArray[16][16][4];
    for (int i = 0; i < 16; i++)
    {
        for (int j = 0; j < 16; j++)
        {
            if ((i + j) % 2 == 0)
            {
                texArray[i][j][0] = 1;
                texArray[i][j][1] = 1;
                texArray[i][j][2] = 1;
                texArray[i][j][3] = 1;
            }
            else
            {
                texArray[i][j][0] = 0;
                texArray[i][j][1] = 0;
                texArray[i][j][2] = 0;
                texArray[i][j][3] = 1;
            }
        }
    }

    // 指定边界混合模式
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    // 指定二维纹理图像
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_FLOAT, texArray);

    // 激活纹理
    glEnable(GL_TEXTURE_2D);
}

void render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // 绘制纹理线段
    // glBegin(GL_LINES);
    // glTexCoord1f(0);
    // glVertex3f(0, 0, 0);
    // glTexCoord1f(1);
    // glVertex3f(1, 1, 0);
    // glEnd();

    glBegin(GL_LINES);
    glTexCoord1f(0);
    glVertex3f(0, 0, 0);
    glTexCoord1f(10);
    glVertex3f(1, 1, 0);
    glEnd();

    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glColor3f(1, 0, 0);
    glVertex3f(0, 0, 0);
    glTexCoord2f(1, 0);
    glColor3f(0, 1, 0);
    glVertex3f(1, 0, 0);
    glTexCoord2f(1, 1);
    glColor3f(0, 0, 1);
    glVertex3f(1, 1, 0);
    glTexCoord2f(0, 1);
    glColor3f(1, 1, 0);
    glVertex3f(0, 1, 0);
    glEnd();
}

int main()
{
    if (!glfwInit())
    {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return -1;
    }

    GLFWwindow *window = glfwCreateWindow(800, 600, "Three Stars", nullptr, nullptr);
    if (!window)
    {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    // 注册函数
    glfwSetFramebufferSizeCallback(window, GLFWWindowResize);
    glfwSetScrollCallback(window, GLFWScrollCallback);
    glfwSetMouseButtonCallback(window, GLFWMouseButtonCallback);
    glfwSetCursorPosCallback(window, GLFWCursorPosCallback);
    glfwSetKeyCallback(window, GLFWKeyCallback);
    glfwSetDropCallback(window, GLFWDropCallback);

    init();
    while (!glfwWindowShouldClose(window))
    {
        // 视图变换
        transform(window);

        render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();

    return 0;
}
